#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import json

import requests
from requests import session

session = requests.session()

baseUrl = ""
username = ""
password = ""


# login
def login(username, password):
    body = {
        "username": username,
        "password": password,
        "login-submit": "登录"
    }
    loginUrl = baseUrl + "/signin"
    resp = session.post(loginUrl, body)
    if resp.status_code != 200:
        raise Exception("Login failed")
    pass

def apps():
    apps_url = baseUrl + "/apps"
    resp = session.get(apps_url)
    return resp.json()
    pass

# appList
def applist():
    applisturl = baseUrl + "/apps/by-owner"
    body = {
        "owner": username,
        "page": 0,
        "size": 600
    }
    resp = session.request("get", applisturl, body)
    return resp.json()
    pass

def getDetail(app_json_list):
#     envs = ["DEV", "UAT1", "STG", "PRD"]
#     envs = ["UAT1", "STG", "PRD"]
    envs = ["STG", "PRD"]
#     envs = ["STG"]
    # envs = {"PRD"}
    for app in app_json_list:
        item_total_count_env = 0
        instance_total = 0
        dataChangeLastModifiedTime = ''
        # print(app['appId'])
        for env in envs:
#             item_total_count_env += namespace(app['appId'], env)
            instance_total += namespace_instance_total(app['appId'], env)
#             tmp = namespace_dataChangeLastModifiedTime(app['appId'], env)
#             if tmp.lower() > dataChangeLastModifiedTime.lower():
#                 dataChangeLastModifiedTime = tmp
#         if item_total_count_env == 0 and instance_total == 0: # and dataChangeLastModifiedTime.lower() < '2025-01-01'.lower():
        if instance_total == 0: # and dataChangeLastModifiedTime.lower() < '2025-01-01'.lower():
            print(app['appId'])
        pass

    pass

def namespace(appId, env):
    namespace_url = baseUrl + "/apps/" + appId + "/envs/" + env + "/clusters/default/namespaces"
    resp = session.get(namespace_url)
    namespaces = resp.json()
    instance_total_count = 0
    item_total_count = 0
    for namespace in namespaces:
        # print(appId, namespace['baseInfo']['namespaceName'])
        try:
            item_total_count += len(namespace['items'])
        except:
            print('0' * 10, appId)
        # print(namespace['baseInfo']['namespaceName'])
        instance_total_count += instancecount(appId, env, namespace['baseInfo']['namespaceName'])
        # commit(appId, env, namespace['baseInfo']['namespaceName'])

    # if instance_total_count == 0:
        # print(appId, env, instance_total_count)
        # print(appId,"\t", namespaces[0]['baseInfo']['dataChangeLastModifiedTime'])
    # print()
    # if item_total_count ==0:
    #     print(appId)
    return item_total_count
    pass

def namespace_instance_total(appId, env):
    namespace_url = baseUrl + "/apps/" + appId + "/envs/" + env + "/clusters/default/namespaces"
    resp = session.get(namespace_url)
    namespaces = resp.json()
    instance_total_count = 0
    item_total_count = 0
    for namespace in namespaces:
        # print(appId, namespace['baseInfo']['namespaceName'])
#         try:
#             item_total_count += len(namespace['items'])
#         except:
#             print('0' * 10, appId)
        # print(namespace['baseInfo']['namespaceName'])
        instance_total_count += instancecount(appId, env, namespace['baseInfo']['namespaceName'])
        # commit(appId, env, namespace['baseInfo']['namespaceName'])

    # if instance_total_count == 0:
        # print(appId, env, instance_total_count)
        # print(appId,"\t", namespaces[0]['baseInfo']['dataChangeLastModifiedTime'])
    # print()
    # if item_total_count ==0:
    #     print(appId)
    return instance_total_count
    pass

def namespace_dataChangeLastModifiedTime(appId, env):
    namespace_url = baseUrl + "/apps/" + appId + "/envs/" + env + "/clusters/default/namespaces"
    resp = session.get(namespace_url)
    namespaces = resp.json()
    # print(len(namespaces))
    # instance_total_count = 0
    item_total_count = 0
    for namespace in namespaces:
        # print(appId, namespace['baseInfo']['namespaceName'])
        try:
          item_total_count += len(namespace['items'])
        except:
            print('0' * 10, appId)
        # print(namespace['baseInfo']['namespaceName'])
        # instance_total_count += instancecount(appId, env, namespace['baseInfo']['namespaceName'])
        dataChangeLastModifiedTime = commit(appId, env, namespace['baseInfo']['namespaceName'])
#         print(appId, "\t", dataChangeLastModifiedTime)

    # if instance_total_count == 0:
        # print(appId, env, instance_total_count)
        # print(appId,"\t", namespaces[0]['baseInfo']['dataChangeLastModifiedTime'])
    # print()
    # if item_total_count ==0:
    #     print(appId)
    return dataChangeLastModifiedTime
    pass

def instancecount(appId, env, namespaceName):
    instancecount_url = baseUrl + "/envs/" + env + "/instances/by-namespace/count"
    body = {
        "appId": appId,
        "clusterName": "default",
        "namespaceName": namespaceName
    }
    resp = session.request("get", instancecount_url, body)
    return resp.json()['num']
    pass

def commit(appId, env, namespace):
    commit_url = baseUrl + "/apps/" + appId + "/envs/" + env + "/clusters/default/namespaces/" + namespace + "/commits?page=0&size=10"
    resp = session.get(commit_url)
#     print(resp.json())
    dataChangeCreatedTime = ''
    if len(resp.json()) > 0:
        dataChangeCreatedTime = resp.json()[0]['dataChangeLastModifiedTime']
#         if dataChangeCreatedTime > '2024' and resp.json()[0]['dataChangeCreatedBy'] != 'apollo':
#             print(appId, env, resp.json()[0]['dataChangeCreatedTime'], resp.json()[0]['dataChangeCreatedBy'])
         # 可注释掉
    return dataChangeCreatedTime
    pass

# def deleteApp(appId):
#     skipList = {"p-third-coupon-b", "p-third-coupon-e"}
#     if skipList.__contains__(appId):
#         return
#     deleteurl = baseUrl + "/apps/" + appId
# #     print("delete resp",deleteurl)
#     resp = session.request("delete", deleteurl)
#     print("delete resp",deleteurl, resp)
# #     print(deleteurl)
#     pass


def getApp(appId):
    appurl = baseUrl + "/apps/" + appId
    resp = session.get(appurl)
    return resp.json()
    pass



def updateOwner(app):
    update_ownerurl = baseUrl + "/apps/" + app['appId']
    app['ownerName'] = username
    headers = {'Content-Type': 'application/json'}
    app_json = json.dumps(app)
    resp = session.request("put", update_ownerurl, data=app_json, headers = headers)
    print(resp)
    pass

def master(appId):
    master_url = baseUrl + "/apps/" + appId + "/roles/Master"
    resp = session.request("post", master_url, data = username)
    print(resp.text)
    pass

def main():
    login(username, password)

    app_json_list = apps()
    # for app in app_json_list:
    #     master(app['appId'])
    #     updateOwner(app)
    # app = getApp("aegean-property-e-rest")
    # app_json_list = applist()
#     app_json_list = [{ "appId": "gateway20181109"}]
    getDetail(app_json_list)

#     applist = ["aimall-wty-user-service", "aimall-wty-boot-admin", "aimall-wty-finance-e-rest", "aimall-wty-investment-e-rest", "aimall-wty-operation-e-rest", "aimall-wty-operation-b-rest", "aimall-wty-property-b-rest", "aimall-wty-property-e-rest", "aimall-wty-pay-b-rest", "aimall-wty-pay-e-rest", "aimall-wty-common-rest", "aimall-wty-job", "aimall-wty-report-server", "aimall-wty-admin-rest", "aimall-wty-business-rest", "aimall-wty-gateway", "aimall-wty-map-creator", "aimall-wty-map-file", "aimall-wty-map-sdk", "aimall-wty-park-rest", "common-aimall-wty", "aimall-ffep-user-service", "aimall-ffep-finance-e-rest", "aimall-ffep-investment-e-rest", "aimall-ffep-operation-e-rest", "aimall-ffep-operation-b-rest", "aimall-ffep-property-b-rest", "aimall-ffep-property-e-rest", "aimall-ffep-pay-b-rest", "aimall-ffep-pay-e-rest", "aimall-ffep-common-rest", "aimall-ffep-job", "aimall-ffep-report-server", "aimall-ffep-admin-rest", "aimall-ffep-business-rest", "aimall-ffep-gateway", "aimall-ffep-park-rest", "common-aimall-ffep", "aimall-ffep-map-creator", "aimall-ffep-map-file", "aimall-ffep-map-sdk", "aimall-jianji-gateway", "aimall-jianji-admin-rest", "aimall-jianji-business-rest", "aimall-jianji-park-rest", "aimall-jian-user-service", "aimall-jian-finance-e-rest", "aimall-jian-investment-e-rest", "aimall-jian-operation-e-rest", "aimall-jian-operation-b-rest", "aimall-jian-property-b-rest", "aimall-jian-property-e-rest", "aimall-jian-pay-b-rest", "aimall-jian-pay-e-rest", "aimall-jian-common-rest", "aimall-jian-job", "aimall-jian-report-server", "aimall-jian-admin-rest", "aimall-jian-business-rest", "aimall-jian-gateway", "aimall-jian-park-rest", "common-aimall-jian", "aimall-jian-boot-admin", "aimall-tlw-user-service", "aimall-tlw-finance-e-rest", "aimall-tlw-investment-e-rest", "aimall-tlw-operation-e-rest", "aimall-tlw-operation-b-rest", "aimall-tlw-property-b-rest", "aimall-tlw-property-e-rest", "aimall-tlw-pay-b-rest", "aimall-tlw-pay-e-rest", "aimall-tlw-common-rest", "aimall-tlw-job", "aimall-tlw-report-server", "aimall-tlw-admin-rest", "aimall-tlw-business-rest", "aimall-tlw-gateway", "aimall-tlw-map-creator", "aimall-tlw-map-file", "aimall-tlw-map-sdk", "aimall-tlw-park-rest", "common-aimall-tlw", "aimall-tlw-boot-admin", "aimall-base-user-e-rest", "aimall-base-user-b-rest", "aimall-base-user-service", "aimall-base-finance-e-rest", "aimall-base-investment-e-rest", "aimall-base-operation-e-rest", "aimall-base-operation-b-rest", "aimall-base-property-b-rest", "aimall-base-property-e-rest", "aimall-base-pay-b-rest", "aimall-base-pay-e-rest", "aimall-base-file-server", "aimall-base-message-service", "aimall-base-workflow-server", "aimall-base-job", "aimall-base-report-server", "aimall-base-admin-rest", "aimall-base-business-rest", "aimall-base-gateway", "aimall-base-park-rest", "common-aimall-base", "aimall-base-crm-test", "aimall-base-common-rest", "aimall-base-map-creator", "aimall-base-map-file", "aimall-base-map-sdk", "aimall-base-boot-admin", "aimall-base-tduck-rest", "aimall-base-from-rest", "aimall-base-rsdata-marketing", "aimall-base-form-rest", "aimall-hexin-admin-rest", "aimall-hexin-business-rest", "aimall-hexin-gateway", "aimall-hexin-park-rest", "common-aimall-hexi", "aimall-hexi-user-e-rest", "aimall-hexi-user-b-rest", "aimall-hexi-user-service", "aimall-hexi-file-server", "aimall-hexi-message-service", "aimall-hexi-workflow-server", "aimall-hexi-finance-e-rest", "aimall-hexi-investment-e-rest", "aimall-hexi-job", "aimall-hexi-operation-e-rest", "aimall-hexi-operation-b-rest", "aimall-hexi-property-e-rest", "aimall-hexi-property-b-rest", "aimall-hexi-report-server", "aimall-hexi-pay-e-rest", "aimall-hexi-pay-b-rest", "aimall-hexi-proprietary-e-rest", "aimall-hexi-map-creator", "aimall-hexi-map-file", "aimall-hexi-map-sdk", "aimall-wansg-investment-e-rest", "aimall-wansg-operation-e-rest", "aimall-wansg-operation-b-rest", "aimall-wansg-pay-e-rest", "aimall-wansg-pay-b-rest", "aimall-wansg-finance-e-rest", "aimall-wansg-property-e-rest", "aimall-wansg-property-b-rest", "aimall-wansg-user-service", "aimall-wansg-user-e-rest", "aimall-wansg-user-b-rest", "aimall-wansg-file-server", "aimall-wansg-workflow", "aimall-wansg-message", "aimall-wansg-job", "aimall-wansg-report-server", "aimall-wansg-admin-rest", "aimall-wansg-gateway", "aimall-wansg-business-rest", "aimall-wansg-park-rest", "aimall-wansg-workflow-server", "aimall-wansg-message-service", "aimall-fuli-investment-e-rest", "aimall-fuli-operation-e-rest", "aimall-fuli-operation-b-rest", "aimall-fuli-pay-e-rest", "aimall-fuli-pay-b-rest", "aimall-fuli-finance-e-rest", "aimall-fuli-property-e-rest", "aimall-fuli-property-b-rest", "aimall-fuli-user-service", "aimall-fuli-user-e-rest", "aimall-fuli-user-b-rest", "aimall-fuli-file-server", "aimall-fuli-workflow-server", "aimall-fuli-message-service", "aimall-fuli-job", "aimall-fuli-report-server", "common-aimall-fuli", "aimall-zhongao-investment-e-rest", "aimall-zhongao-operation-e-rest", "common-aimall-zhongao", "aimall-zhongao-operation-b-rest", "aimall-zhongao-pay-e-rest", "aimall-zhongao-pay-b-rest", "aimall-zhongao-finance-e-rest", "aimall-zhongao-property-e-rest", "aimall-zhongao-property-b-rest", "aimall-zhongao-user-service", "aimall-zhongao-user-e-rest", "aimall-zhongao-user-b-rest", "aimall-zhongao-file-server", "aimall-zhongao-workflow-server", "aimall-zhongao-message-service", "aimall-zhongao-job", "aimall-zhongao-report-server", "aimall-huixiang-user-e-rest", "aimall-huixiang-user-b-rest", "aimall-huixiang-user-service", "aimall-huixiang-file-server", "aimall-huixiang-message-service", "aimall-huixiang-workflow-server", "aimall-huixiang-finance-e-rest", "aimall-huixian-investment-e-rest", "aimall-huixiang-job", "aimall-huixiang-operation-e-rest", "aimall-huixiang-operation-b-rest", "aimall-huixiang-property-e-rest", "aimall-huixiang-property-b-rest", "aimall-huixiang-report-server", "aimall-huixiang-pay-e-rest", "aimall-huixiang-pay-b-rest", "aimall-huixiang-gateway", "aimall-huixiang-admin-rest", "aimall-huixiang-business-rest", "aimall-huixia-proprietary-e-rest", "aimall-huixiang-park-rest", "aimall-huixiang-merchant-b-rest", "common-aimall-yuanzu", "saas-property-yuanzu-rest", "common-redstar-erp", "redstar-erp-park-rest", "redstar-erp-merchant-rest", "aimall-xingyao-activity-b-rest", "aimall-xingyao-activity-e-rest", "bgy-file-server", "bgy-investment-e-rest", "bgy-operation-e-rest", "bgy-workflow-server", "bgy-operation-b-rest", "bgy-finance-e-rest", "bgy-property-e-rest", "bgy-property-b-rest", "bgy-message-service", "bgy-job", "bgy-report-server", "bgy-proprietary-e-rest", "bgy-proprietary-b-rest", "bgy-pay-b-rest", "bgy-pay-e-rest", "bgy-finance-b-rest", "changjian-file-server", "changjian-finance-e-rest", "changjian-investment-e-rest", "changjian-job", "changjian-message-service", "changjian-operation-e-rest", "changjian-operation-b-rest", "changjian-property-e-rest", "changjian-property-b-rest", "changjian-report-server", "changjian-user-e-rest", "changjian-user-b-rest", "changjian-user-service", "changjian-workflow-server", "common-xindi", "xindi-user-e-rest", "xindi-user-b-rest", "xindi-user-service", "xindi-investment-e-rest", "xindi-operation-e-rest", "xindi-operation-b-rest", "xindi-pay-e-rest", "xindi-pay-b-rest", "xindi-finance-e-rest", "xindi-property-e-rest", "xindi-property-b-rest", "xindi-workflow-server", "xindi-file-server", "xindi-message-service", "xindi-report-server", "xindi-job", "xindi-boot-admin", "hexin-pay-e-rest", "hexin-investment-e-rest", "hexin-operation-e-rest", "hexin-operation-b-rest", "hexin-pay-b-rest", "hexin-finance-e-rest", "hexin-property-e-rest", "hexin-property-b-rest", "hexin-user-service", "hexin-user-b-rest", "hexin-file-server", "hexin-workflow", "hexin-message", "hexin-job", "hexin-report-server", "common-hexin", "aimall-xyxc-business-rest", "aimall-xyxc-gateway", "jianji-job", "jianji-user-e-rest", "jianji-user-b-rest", "jianji-user-service", "jianji-investment-e-rest", "jianji-operation-e-rest", "jianji-operation-b-rest", "jianji-pay-e-rest", "jianji-pay-b-rest", "jianji-finance-e-rest", "jianji-property-e-rest", "jianji-property-b-rest", "jianji-workflow-server", "jianji-file-server", "jianji-message-service", "common-jianji", "jianji-report-server", "aimall-mlgz-coupon-b-rest", "aimall-mlgz-member-b", "aimall-mlgz-wechat-b-rest", "aimall-mlgz-wechat-e-rest", "aimall-mlgz-operation-b-rest", "aimall-mlgz-operation-c-rest", "aimall-mlgz-operation-e-rest", "aimall-mlgz-park-s-rest", "aimall-mlgz-park-e-rest", "aimall-mlgz-member-b-rest", "aimall-demo-coupon-e-rest", "aimall-demo-member-e-rest", "aimall-demo-map-e-rest", "aimall-demo-gateways", "aimall-demo-operation", "aimall-demo-operation-e-rest", "aimall-demo-operation-b-rest", "aimall-demo-operation-c-rest", "aimall-demo-member-c-rest", "aimall-demo-member-b-rest", "aimall-demo-coupon-c-rest", "aimall-demo-coupon-b-rest", "aimall-demo-applet-b-rest", "aimall-demo-applet-c-rest", "aimall-demo-applet-e-rest", "aimall-demo-business-rest", "common-cztfjt", "cztfjt-user-e-rest", "cztfjt-user-b-rest", "cztfjt-user-service", "cztfjt-investment-e-rest", "cztfjt-finance-e-rest", "cztfjt-operation-e-rest", "cztfjt-operation-b-rest", "cztfjt-property-e-rest", "cztfjt-property-b-rest", "cztfjt-workflow-server", "cztfjt-message-service", "cztfjt-report-server", "cztfjt-pay-e-rest", "cztfjt-pay-b-rest", "cztfjt-file-server", "cztfjt-job", "common-gaoli", "gaoli-operation-e-rest", "gaoli-operation-b-rest", "gaoli-user-service", "gaoli-user-b-rest", "gaoli-user-e-rest", "gaoli-investment-e-rest", "gaoli-finance-e-rest", "gaoli-file-server", "gaoli-job", "gaoli-procurement-e-rest", "aimall-gaoli-coupon-e-rest", "aimall-gaoli-coupon-c-rest", "aimall-gaoli-coupon-b-rest", "aimall-gaoli-member-e-rest", "aimall-gaoli-member-b-rest", "aimall-gaoli-member-c-rest", "aimall-gaoli-applet-e-rest", "gaoli-operation-c-rest", "aimall-gaoli-wechat-e-rest", "aimall-gaoli-wechat-b-rest", "aimall-gaoli-wechat-c-rest", "mall-gaoli-user-service", "mall-gaoli-user-e-rest", "mall-gaoli-user-b-rest", "common-gaoli-mall", "mall-gaoli-file-server", "mall-gaoli-investment-e-rest", "mall-gaoli-finance-e-rest", "mall-gaoli-operation-e-rest", "mall-gaoli-operation-b-rest", "mall-gaoli-pay-b-rest", "mall-gaoli-property-e-rest", "mall-gaoli-property-b-rest", "mall-gaoli-pay-e-rest", "mall-gaoli-message-service", "mall-gaoli-report-server", "mall-gaoli-workflow-server", "mall-gaoli-job", "gaoli-pay-e-rest", "gaoli-pay-b-rest", "gaoli-pay-job", "common-jiazhou", "jiazhou-investment-e-rest", "jiazhou-operation-e-rest", "jiazhou-operation-b-rest", "jiazhou-pay-e-rest", "jiazhou-pay-b-rest", "jiazhou-finance-e-rest", "jiazhou-property-e-rest", "jiazhou-property-b-rest", "jiazhou-user-service", "jiazhou-user-e-rest", "jiazhou-user-b-rest", "jiazhou-file-server", "jiazhou-workflow", "jiazhou-message", "jiazhou-job", "jz-common", "rsc-bdm-e-rest", "all-in-one", "arch-monitor", "mlab-open-rest", "mlab-b-rest", "isps-web", "live-c-rest", "live-e-rest", "live-job", "jiazhou-report-server", "aimall-jz-member-e-rest", "aimall-jz-member-c-rest", "aimall-jz-member-b-rest", "aimall-jz-wechat-e-rest", "aimall-jz-coupon-e-rest", "aimall-jz-coupon-c-rest", "aimall-jz-coupon-b-rest", "aimall-jz-wechat-c-rest", "aimall-jz-wechat-b-rest", "aimall-jz-operation-e-rest", "aimall-jz-operation-b-rest", "aimall-jz-operation-c-rest", "aimall-jz-park-e-rest", "aimall-jz-park-s-rest", "aimall-jz-park-b-rest", "aimall-jz-gateway", "zendai-file-server", "common-zendai", "zendai-job", "zendai-message-service", "zendai-investment-e-rest", "zendai-property-job", "zendai-property-b-rest", "zendai-property-e-rest", "zendai-pay-e-rest", "zendai-pay-b-rest", "zendai-pay-job", "zendai-finance-e-rest", "zendai-operation-e-rest", "zendai-operation-b-rest", "zendai-workflow-server", "zendai-user-service", "zendai-user-e-rest", "zendai-user-b-rest", "zendai-report-server", "zendai-activity-e-rest", "zendai-activity-b-rest", "aimall-saas-marketing-m-rest", "aimall-saas-marketing-c-rest", "aimall-saas-job-rest", "aimall-saas-member-m-rest", "aimall-saas-operation-m-rest", "aimall-saas-message", "aimall-saas-file", "aimall-saas-gateway", "aimall-saas-operation-c-rest", "aimall-solution-applet-c-rest", "aimall-solution-applet-e-rest", "aimall-solution-map-e-rest", "aimall-solution-applet-b-rest", "aimall-solution-report-web-e", "aimall-solution-operation-web-c", "aimall-solution-operation-web-e", "aimall-solution-operation-web-b", "aimall-solution-operation-job", "saas-investment-e-rest", "saas-investment-job", "saas-investment-mq", "saas-operation-e-rest", "saas-user-service", "saas-user-e-rest", "saas-user-b-rest", "saas-user-job", "saas-user-mq", "saas-operation-b-rest", "saas-operation-job", "saas-operation-mq", "saas-finance-e-rest", "saas-contract-e-rest", "saas-common", "saas-contract-job", "saas-contract-mq", "saas-finance-mq", "saas-finance-job", "saas-message-service", "saas-message-mq", "saas-fileserver", "saas-manage-user-e-rest", "saas-manage-user-job", "saas-job", "saas-investment-b-rest", "saas-finance-b-rest", "saas-workflow-server", "saas-form-server", "saas-member-c-rest", "saas-report-server", "nova-finance-e-rest", "nova-finance-job", "nova-user-service", "nova-user-e-rest", "nova-user-job", "nova-investment-e-rest", "nova-investment-job", "nova-administration-e-rest", "nova-investment-mq", "nova-finance-mq", "nova-user-mq", "nova-operation-e-rest", "nova-process-e-rest", "nova-file-server", "rsclouds-web-sample", "user-authority-rest", "mklinfo-homePage-v2", "mklinfo-homePage"]
#     for app in applist:
#         deleteApp(app)
#     pass

if __name__ == '__main__':
    main()
